package com.guigu.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.eduservice.entity.Article;
import com.guigu.eduservice.entity.ArticleCategory;
import com.guigu.eduservice.mapper.ArticleMapper;
import com.guigu.eduservice.service.ArticleCategoryService;
import com.guigu.eduservice.service.ArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.eduservice.vo.ArticleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 文章表（存储平台发布的资讯、指南等内容） 服务实现类
 * </p>
 *
 * @author janecraft
 * @since 2025-09-27
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleCategoryService articleCategoryService;

    //查询文章表和分类的连接查询
    @Override
    public PageInfo<Article> getArticlePage(Integer PageNo, Integer PageSize, ArticleVo vo) {

        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        if (vo.getCategoryIdVo()!=null){
            wrapper.eq("category_id",vo.getCategoryIdVo());
        }
        PageHelper.startPage(PageNo,PageSize);
        List<Article> list = articleService.list(wrapper);
            for (int i=0;i<list.size();i++){
                Article article = list.get(i);
                ArticleCategory category = articleCategoryService.getById(article.getCategoryId());
                article.setArticleCategories(category);
            }
        PageInfo<Article> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
